from itsdangerous import TimedJSONWebSignatureSerializer as TJSWSerializer
from django.conf import  settings
#1.加密openid
def generate_sign_openid(openid):
    #1.创建TJSWSerializer
    serializer = TJSWSerializer(secret_key=settings.SECRET_KEY,expires_in=3000)

    #2.加密数据
    sign_openid = serializer.dumps({"openid":openid})

    #返回数据
    return sign_openid.decode()


#2.解密openid
def decode_sign_openid(sign_openid):
    # 1.创建TJSWSerializer
    serializer = TJSWSerializer(secret_key=settings.SECRET_KEY, expires_in=3000)

    # 2.解密数据
    try:
        dict_data = serializer.loads(sign_openid)
    except Exception as e:
        return  None
    # 返回数据

    return dict_data.get("openid")
